package link.anyauto.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;

/**
 * Created by Administrator on 2016/5/5.
 */
public class UserInfoProvider extends ContentProvider {

    private static final String CONTENT = "content://";
    public static final String AUTHORITY = "link.anyauto.provider";
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd." + AUTHORITY;
    public static final String CONTENT_TYPE_ITEM = "vnd.android.cursor.item/vnd." + AUTHORITY;

    public static final Uri POST_CODE_URI = Uri.parse(CONTENT + AUTHORITY + "/" + UserInfoDbHelper.TABLE_USER_INFO);
    public static final Uri COMPANY_URI = Uri.parse(CONTENT + AUTHORITY + "/" + UserInfoDbHelper.TABLE_COMPANY);

    SQLiteDatabase database;
    static final int USER_INFOS = 1;
    static final int USER_INFO_ITEM = 2;
    static final int COMPANY = 3;
    static final int COMPANY_ITEM = 4;

    static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        matcher.addURI(AUTHORITY, "userinfo", USER_INFOS);
        matcher.addURI(AUTHORITY, "userinfo/*", USER_INFO_ITEM);
        matcher.addURI(AUTHORITY, "company", COMPANY);
        matcher.addURI(AUTHORITY, "company/#", COMPANY_ITEM);
    }

    @Override
    public boolean onCreate() {
        database = new UserInfoDbHelper(getContext()).getWritableDatabase();
        return true;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;
        switch (matcher.match(uri)) {
            case USER_INFOS:
                cursor = database.query(UserInfoDbHelper.TABLE_USER_INFO, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case USER_INFO_ITEM:
                String tel = uri.getPathSegments().get(1);
                cursor = database.query(UserInfoDbHelper.TABLE_USER_INFO, projection, "tel_num = ?", new String[]{tel}, null, null, sortOrder);
                break;
            case COMPANY:
                cursor = database.query(UserInfoDbHelper.TABLE_COMPANY, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case COMPANY_ITEM:
                String cid = uri.getPathSegments().get(0);
                cursor = database.query(UserInfoDbHelper.TABLE_COMPANY, projection, "id = ?", new String[]{cid}, null, null, sortOrder);
                break;
        }
        return cursor;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        String type = null;
        switch (matcher.match(uri)) {
            case USER_INFOS:
            case COMPANY:
                type = CONTENT_TYPE;
                break;
            case USER_INFO_ITEM:
            case COMPANY_ITEM:
                type = CONTENT_TYPE_ITEM;
                break;
        }
        return type;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        long newId = 0;
        Uri newUri = null;
        switch (matcher.match(uri)) {
            case USER_INFOS:
                newId = database.insert(UserInfoDbHelper.TABLE_USER_INFO, null, contentValues);
                newUri = Uri.parse(CONTENT + AUTHORITY + "/" + UserInfoDbHelper.TABLE_USER_INFO + "/" + newId);
                break;
            case COMPANY:
                newId = database.insert(UserInfoDbHelper.TABLE_COMPANY, null, contentValues);
                newUri = Uri.parse(CONTENT + AUTHORITY + "/" + UserInfoDbHelper.TABLE_COMPANY + "/" + newId);
                break;
        }
        if(newId > 0) {
            return newUri;
        }
        throw new RuntimeException("无法插入行：" + uri + "，尝试插入内容：" + contentValues);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int rows = 0;
        switch (matcher.match(uri)) {
            case USER_INFOS:
                rows = database.delete(UserInfoDbHelper.TABLE_USER_INFO, selection, selectionArgs);
                break;
            case USER_INFO_ITEM:
                rows = database.delete(UserInfoDbHelper.TABLE_USER_INFO, "tel_num = ?", new String[] {uri.getPathSegments().get(0)});
                break;
            case COMPANY:
                rows = database.delete(UserInfoDbHelper.TABLE_COMPANY, selection, selectionArgs);
                break;
            case COMPANY_ITEM:
                rows = database.delete(UserInfoDbHelper.TABLE_COMPANY, "id = ?", new String[]{uri.getPathSegments().get(0)});
                break;
        }
        return rows;
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
        int rows = 0;
        switch (matcher.match(uri)) {
            case USER_INFOS:
                rows = database.update(UserInfoDbHelper.TABLE_USER_INFO, contentValues, selection, selectionArgs);
                break;
            case USER_INFO_ITEM:
                rows = database.update(UserInfoDbHelper.TABLE_USER_INFO, contentValues, "tel_num = ?", new String[]{uri.getPathSegments().get(0)});
                break;
            case COMPANY:
                rows = database.update(UserInfoDbHelper.TABLE_COMPANY, contentValues, selection, selectionArgs);
                break;
            case COMPANY_ITEM:
                rows = database.update(UserInfoDbHelper.TABLE_COMPANY, contentValues, "id = ?", new String[]{uri.getPathSegments().get(0)});
                break;
        }
        return rows;
    }
}
